/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaWindow.java
 *
 * Created on 12/10/2011, 11:48:47 PM
 */

package bolsavalorappclient;

import bean.BeanAccionRemote;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import java.util.Properties;
import java.util.TimerTask;
import java.util.Timer;

/**
 *
 * @author ari
 */
public class ConsultaWindow extends javax.swing.JFrame {

    private static BeanAccionRemote accion;
    
    TimerTask timerTask = new TimerTask() {
        public void run() {
            refresh();
        }
    };
    
    /** Creates new form ConsultaWindow */
    public ConsultaWindow(Properties properties) throws Exception{
        initComponents();
        jList1.setModel(new DefaultListModel());
        model = (DefaultListModel) jList1.getModel();
        try{
            this.properties = properties;

            accion = lookupBeanAccionRemote();

            refresh();
        }catch (Exception e){
            JOptionPane.showMessageDialog(null, "Se perdió conexión con el "
                    + "servidor", "Advertencia!!",
                    JOptionPane.INFORMATION_MESSAGE);
            dispose();
            throw new Exception();
        }
        
        /* Timer que refresca la lista cada cierto tiempo */
        this.addWindowListener(new WindowAdapter() 
        {
          public void windowClosed(WindowEvent e)
          {
            timerTask.cancel();
          }
        });
                
        /* Se coloca un timer para que se refresquen cada cierto tiempo */
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(timerTask, 0, 60 * 1000);     // Cada minuto    
    }

    private void refresh() {
        // Se obtienen la lista de empresas con acciones en la base de datos

        model.clear();
       
        List<String> empresas = accion.getEmpresas();
        
        for(String temp : empresas) {
            model.addElement(temp);
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta");

        jPanel1.setBackground(new java.awt.Color(187, 179, 237));

        jLabel1.setText("Empresas:");

        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE)
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        // TODO add your handling code here:
        new EmpresaDialog(this, true, jList1.getSelectedValue().toString(), properties).setVisible(true);
    }//GEN-LAST:event_jList1MouseClicked

    private DefaultListModel model;

    private Properties properties;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private BeanAccionRemote lookupBeanAccionRemote() {
        try {
            Context c = new InitialContext(properties);
            return (BeanAccionRemote) c.lookup("java:comp/env/BeanAccion");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
